package com.heliang.ui;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.annotation.view.ViewInject;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.example.appxiehui2.R;
import com.heliang.dataaccess.bean.PartInfoBean;
import com.heliang.ui.info.IndexActivity;
import com.heliang.ui.member.MemberIndexActivity;
import com.heliang.ui.more.MoreIndexActivity;
import com.heliang.ui.store.StoreIndexActivity;
import com.heliang.ui.trade.TradeIndexActivity;

@SuppressLint("CutPasteId")
public class MainActivity extends TabActivity implements OnCheckedChangeListener {

    private TabHost appTabhost;
    private RadioGroup appMainTab;
    
    // 底部按钮
    @ViewInject(id=R.id.main_tab_rg) private RadioGroup radioGroup;

    // tab的tag
    public static final String INFO = "tab_info";
    public static final String TRADE = "tab_trade";
    public static final String STORE = "tab_store";
    public static final String MEMBER = "tab_member";
    public static final String MORE = "tab_more";

    public static List<PartInfoBean> TAB_PART_LIST = new ArrayList<PartInfoBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // 初始化afinal
        new FinalActivityHelper(this).initView();
        
        radioGroup = (RadioGroup) findViewById(R.id.main_tab_rg);
        radioGroup.setOnCheckedChangeListener(this);

        this.appTabhost = getTabHost();
        Intent infoIntent = new Intent(MainActivity.this, IndexActivity.class);
        Intent tradeIntent = new Intent(MainActivity.this, TradeIndexActivity.class);
        Intent storeIntent = new Intent(MainActivity.this, StoreIndexActivity.class);
        Intent memberIntent = new Intent(MainActivity.this, MemberIndexActivity.class);
        Intent moreIntent = new Intent(MainActivity.this, MoreIndexActivity.class);

        appTabhost.addTab(buildTabSpec("tab_info", "首页", R.drawable.info_icon_selected, infoIntent));
        appTabhost.addTab(buildTabSpec("tab_trade", "单位", R.drawable.info_icon_selected, tradeIntent));
        appTabhost.addTab(buildTabSpec("tab_store", "我", R.drawable.info_icon_selected, storeIntent));
        appTabhost.addTab(buildTabSpec("tab_member", "通讯录", R.drawable.info_icon_selected, memberIntent));
        appTabhost.addTab(buildTabSpec("tab_more", "更多", R.drawable.info_icon_selected, moreIntent));
        
        this.appMainTab = radioGroup;
    }

    private TabHost.TabSpec buildTabSpec(String tag, String resLabel, int resIcon, final Intent content) {
        return this.appTabhost.newTabSpec(tag).setIndicator(resLabel, getResources().getDrawable(resIcon))
            .setContent(content);
    }

    /**
     * 初始化tab
     */
    public void inTab() {

    }

    public void onCheckedChanged(RadioGroup group, int checkedid) {
        try {
            Resources resource = (Resources) getBaseContext().getResources();
            ColorStateList csl;
            ColorStateList cslSel;
            cslSel = (ColorStateList) resource.getColorStateList(R.color.white);
            int[] resArray = { R.drawable.info_icon, R.drawable.trade_icon, R.drawable.store_icon,
                    R.drawable.member_icon, R.drawable.more_icon };
            
            // 设置选中字体颜色，把没选中的恢复
            for (int i = 0; i < group.getChildCount(); i++) {
                RadioButton rb = ((RadioButton) group.getChildAt(i));
                csl = (ColorStateList) resource.getColorStateList(R.color.nav_bottom);
                rb.setTextColor(csl);
                rb.setCompoundDrawablesWithIntrinsicBounds(0, resArray[i], 0, 0);
                rb.setBackgroundResource(R.drawable.buttom_notselected);
            }

            RadioButton rb = null;
            rb = (RadioButton) findViewById(checkedid);
            rb.setBackgroundDrawable(getResources().getDrawable(R.drawable.botton_selected));
            rb.setTextColor(cslSel);
            if (R.id.tab_info == checkedid) {
                rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.info_icon_selected, 0, 0);
            } else if (R.id.tab_trade == checkedid) {
                rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.trade_icon_selected, 0, 0);
            } else if (R.id.tab_store == checkedid) {
                rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.store_icon_selected, 0, 0);
            } else if (R.id.tab_member == checkedid) {
                rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.member_icon_selected, 0, 0);
            } else {
                rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.more_icon_selected, 0, 0);
            }
            
            switch (checkedid) {
                case R.id.tab_info:
                    this.appTabhost.setCurrentTabByTag(INFO);
                    rb = ((RadioButton) this.appMainTab.getChildAt(checkedid));
                    rb.setBackgroundDrawable(getResources().getDrawable(R.drawable.botton_selected));
                    rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.info_icon_selected, 0, 0);
                    break;
                case R.id.tab_trade:
                    this.appTabhost.setCurrentTabByTag(TRADE);
                    rb = ((RadioButton) this.appMainTab.getChildAt(checkedid));

                    rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.trade_icon_selected, 0, 0);
                    break;
                case R.id.tab_store:
                    this.appTabhost.setCurrentTabByTag(STORE);
                    rb = ((RadioButton) this.appMainTab.getChildAt(R.id.tab_store));
                    rb.setBackgroundDrawable(getResources().getDrawable(R.drawable.botton_selected));
                    rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.store_icon_selected, 0, 0);
                    break;
                case R.id.tab_member:
                    this.appTabhost.setCurrentTabByTag(MEMBER);
                    rb = ((RadioButton) this.appMainTab.getChildAt(R.id.tab_member));
                    rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.member_icon_selected, 0, 0);
                    break;
                case R.id.tab_more:
                    this.appTabhost.setCurrentTabByTag(MORE);
                    rb = ((RadioButton) this.appMainTab.getChildAt(R.id.tab_more));
                    rb.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.more_icon_selected, 0, 0);
                    break;
            }
            csl = (ColorStateList) resource.getColorStateList(R.color.white);
            rb.setTextColor(csl);
            rb.setTextColor(getResources().getColor(R.color.white));
            rb.setBackgroundResource(R.drawable.botton_selected);
        } catch (Exception e) {
            // TODO: handle exception
            // FileLog.log("AppMainActivity.onCheckedChanged() " +
            // e.getMessage());
        }

    }

}
